package com.example.elderlycare.controller;


import com.example.elderlycare.controller.entity.DepartmentRequestBody;
import com.example.elderlycare.dao.entity.Department;
import com.example.elderlycare.service.DepartmentService;
import com.example.elderlycare.service.entity.DepartmentResponseBody;
import com.example.elderlycare.service.entity.DepartmentTreeResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Resource
    DepartmentService departmentService;

    @PostMapping
    public Result<?> addDepartment(@Valid @RequestBody DepartmentRequestBody.AddDepartmentRequestBody params) {
        Department department = Department.builder().
                name(params.getName()).
                manager(params.getManager()).
                phone(params.getPhone()).
                fatherId(params.getFatherId()).
                nurseId(params.getNurseId()).
                build();
        boolean success = departmentService.save(department);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @PutMapping
    public Result<?> updateDepartment(@Valid @RequestBody DepartmentRequestBody.UpdateDepartmentRequestBody params) {
        Department department = Department.builder().
                id(params.getId()).
                name(params.getName()).
                manager(params.getManager()).
                phone(params.getPhone()).
                fatherId(params.getFatherId()).
                nurseId(params.getNurseId()).
                build();
        boolean success = departmentService.updateById(department);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @DeleteMapping
    public Result<?> deleteDepartment(@Valid @RequestParam("id") Integer id) {
        boolean success = departmentService.removeById(id);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @GetMapping
    public Result<List<DepartmentResponseBody>> queryDepartment() {
        List<Department> department = departmentService.list();
        if (department == null) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(department.stream().map(item -> {
            DepartmentResponseBody.DepartmentResponseBodyBuilder departmentResponseBodyBuilder = DepartmentResponseBody.builder();
            departmentResponseBodyBuilder.id(item.getId()).name(item.getName());
            return departmentResponseBodyBuilder.build();
        }).collect(Collectors.toList()));
    }

    @GetMapping("/tree")
    public Result<DepartmentTreeResponseBody> getDepartmentTree() {
        DepartmentTreeResponseBody departmentTreeResponseBody = departmentService.getDepartmentTree();
        if (departmentTreeResponseBody == null) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(departmentTreeResponseBody);
    }
}
